﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using NanjingbeiRailwayStation.Core;
using NanjingbeiRailwayStation.Data.ClickHouse.Models;
using NanjingbeiRailwayStation.Data.ClickHouse.Services;
using NanjingbeiRailwayStation.WebApi.ActionFilters;
using NanjingbeiRailwayStation.WebApi.Models;

namespace NanjingbeiRailwayStation.WebApi.Controllers
{

    /// <summary>
    /// 事件报告
    /// </summary>

    [Route("[controller]")]
    [TypeFilter(typeof(LoggingActionFilter))]
    public class EventReportController : ControllerBase
    {
        /// <summary>
        /// 报警事件列表
        /// </summary>
        /// <param name="beginDateTime"></param>
        /// <param name="endDateTime"></param>
        /// <returns></returns>

        [HttpGet("GetAlarmList")]
        public async Task<ResultRespose<List<AlarmInfo>>> GetAlarmList(DateTime beginDateTime, DateTime endDateTime)
        {
            var entities = await new EventReportService().GetAlarmList(beginDateTime,endDateTime);
            return ResultRespose<List<AlarmInfo>>.OKWithData(entities??new List<AlarmInfo>());
        }

        /// <summary>
        /// 获取设备的最新事件
        /// </summary>
        /// <param name="imei"></param>
        /// <returns></returns>
        [HttpGet("GetLastEventNotification")]
        public async Task<ResultRespose<EventNotification>> GetLastEventNotification(string imei)
        {
            var entities = await new EventReportService().GetLastEventNotification(imei);
            return ResultRespose<EventNotification>.OKWithData(entities??new EventNotification());

        }
        /// <summary>

        /// <summary>
        /// 事件记录
        /// </summary>
        /// <param name="imei"></param>
        /// <param name="eventName">参考文档</param>
        /// <param name="dateTimeBegin"></param>
        /// <param name="dateTimeEnd"></param>
        /// <returns></returns>
        [HttpGet("GetEventReport")]
        public async Task<ResultRespose<List<MeterEventReport>>> GetEventReport(string imei, string eventName, DateTime dateTimeBegin, DateTime dateTimeEnd)
        {
            var entities = await new EventReportService().GetEventReport(imei, eventName, dateTimeBegin, dateTimeEnd);
            return ResultRespose<List<MeterEventReport>>.OKWithData(entities);


        }
            /// <summary>
            /// 今日报警次数
            /// </summary>
            /// <returns></returns>

            [HttpGet("GetTodayAlarmFrequency")]
        public async Task<ResultRespose<TodayAlarmFrequency>> GetTodayAlarmFrequency()
        {
           
            var service = new NanjingbeiRailwayStation.Data.ClickHouse.Services.EventReportService();

            var entity= await service.GetTodayAlarmFrequency();

            if (entity != null)
            {
                return ResultRespose<TodayAlarmFrequency>.OKWithData(entity);

            }
            return ResultRespose<TodayAlarmFrequency>.OKWithData(new TodayAlarmFrequency());


        }



        /// <summary>
        /// 塔吊报警数据
        /// </summary>
        /// <returns></returns>

        [HttpGet("GetTowerCraneAlarmData")]
        public async Task<ResultRespose< List< TowerCraneAlarmData>>> GetTowerCraneAlarmData(int size=100)
        {

            var service = new NanjingbeiRailwayStation.Data.ClickHouse.Services.EventReportService();

            var entitys=  await service.GetTowerCraneAlarmData();
             

            return ResultRespose<List<TowerCraneAlarmData>>.OKWithData(entitys);

           
        }

        /// <summary>
        /// 塔吊报警数据的数量(今日的）
        /// </summary>
        /// <returns></returns>
        [HttpGet("GetTowerCraneAlarmCount")]
        public async Task<ResultRespose<ulong>> GetTowerCraneAlarmCount()
        {

            var service = new NanjingbeiRailwayStation.Data.ClickHouse.Services.EventReportService();

            var entitys = await service.GetTowerCraneAlarmCount();

            return ResultRespose<ulong>.OKWithData(entitys);

        }

        /// <summary>
        /// 塔吊报警数据(今日的）
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>

        [HttpGet("GetTowerCraneAlarmDataByPage")]
        public async Task<ResultRespose<List<TowerCraneAlarmData>>> GetTowerCraneAlarmDataByPage(int pageIndex,int pageSize)
        {

            var service = new NanjingbeiRailwayStation.Data.ClickHouse.Services.EventReportService();

            var entitys = await service.GetTowerCraneAlarmDataByPage(pageIndex, pageSize);  

            return ResultRespose<List<TowerCraneAlarmData>>.OKWithData(entitys);

        }


        /// <summary>
        /// 故障设备监控
        /// </summary>
        /// <param name="size"></param>
        /// <returns></returns>

        [HttpGet("GetMonitoringOfAultyEquipment")]
        public async Task<ResultRespose<List<MonitoringOfAultyEquipment>>> GetMonitoringOfAultyEquipment(int size = 100)
        {
            var service = new NanjingbeiRailwayStation.Data.ClickHouse.Services.EventReportService();

            var entitys=await service.GetMonitoringOfAultyEquipment();
             
            return ResultRespose<List<MonitoringOfAultyEquipment>>.OKWithData(entitys);
         

        }

        /// <summary>
        /// 告警类型分布
        /// </summary>
        /// <param name="size"></param>
        /// <returns></returns>

        [HttpGet("GetAlarmTypeDistribution")]
        public async Task<ResultRespose<List<AlarmTypeDistribution>>> GetAlarmTypeDistribution(int size = 100)
        {
            var service = new NanjingbeiRailwayStation.Data.ClickHouse.Services.EventReportService();

            var entitys= await service.GetAlarmTypeDistribution();

            return ResultRespose<List<AlarmTypeDistribution>>.OKWithData(entitys);
            
        }

         

    }
}
